#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define y1 y_alpha_1

using namespace std;

using ll = long long;
using db = double;
using vec = vector<int>;
using pii = pair<int,int>;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-',ch = getchar();
	while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
	return f ? -x : x;
}

const int N = 1e5 + 10;
const int inf = 0x7fffffff;

int n,m;

struct cr {
	int t,l,r,w;
}d[N];

ll dis[N];
vec to;

priority_queue < pair < ll , int > , vector < pair < ll , int > > , greater < pair < ll , int > > > q;

struct segtree {
	int mn[N << 2];
	void pushup(int o) { mn[o] = min(mn[o << 1],mn[o << 1 | 1]); }
	void build(int op,int o = 1,int l = 1,int r = m) {
		if(l == r) return mn[o] = d[l].l + op * d[l].t,void(); int mid = l + r >> 1;
		build(op,o << 1,l,mid); build(op,o << 1 | 1,mid + 1,r); pushup(o);
	}
	void query(int w,int xl,int xr,int o = 1,int l = 1,int r = m) {
		if(mn[o] > w) return; int mid = l + r >> 1; 
		if(l == r) return to.pb(l),void();
		if(xl <= mid) query(w,xl,xr,o << 1,l,mid);
		if(xr > mid) query(w,xl,xr,o << 1 | 1,mid + 1,r);
	}
	void upd(int pos,int o = 1,int l = 1,int r = m) {
		if(l == r) return mn[o] = inf,void(); int mid = l + r >> 1;
		if(pos <= mid) upd(pos,o << 1,l,mid); else upd(pos,o << 1 | 1,mid + 1,r); pushup(o);
	}
}T1,T2;
	
int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,m = in; for(int i = 1;i <= m;i++) d[i].t = in,d[i].l = in,d[i].r = in,d[i].w = in;
	memset(dis,0x3f,sizeof dis); sort(d + 1,d + m + 1,[](cr a,cr b) { return a.t < b.t; }); T1.build(-1); T2.build(1);
	for(int i = 1;i <= m;i++)
		if(d[i].l == 1) 
			dis[i] = d[i].w, T1.upd(i), T2.upd(i), q.push(make_pair(dis[i],i));
	while(!q.empty()) {
		int x = q.top().se; ll dd = q.top().fi;
		if(d[x].r == n) return printf("%lld\n",dis[x]),0; vec tmp; to.swap(tmp); q.pop(); if(dis[x] < dd) continue;
		if(x > 1) T1.query(d[x].r - d[x].t + 1,1,x - 1);
		if(x < m) T2.query(d[x].r + d[x].t + 1,x + 1,m);
		for(auto y : to)
			if(dis[y] > dis[x] + d[y].w) {
				dis[y] = dis[x] + d[y].w; T1.upd(y); T2.upd(y); q.push(make_pair(dis[y],y));
			}
	}
	puts("-1");
	return 0;
}
